// 抽象工厂（TypeScript 版本）
// - 提供两组产品示例：主题组件（Button / Input）和请求（GET / POST）
// - 使用抽象类/接口保证类型安全

// ==================== 主题工厂 ====================
export abstract class ThemeFactory {
	abstract createButton(): Button;
	abstract createInput(): Input;
}

export abstract class Button {
	abstract render(): void;
}

export abstract class Input {
	abstract render(): void;
}

export class DarkButton extends Button {
	render(): void {
		console.log('渲染黑色按钮，文字白色');
	}
}

export class DarkInput extends Input {
	render(): void {
		console.log('渲染黑色输入框，文字白色');
	}
}

export class LightButton extends Button {
	render(): void {
		console.log('渲染白色按钮，文字黑色');
	}
}

export class LightInput extends Input {
	render(): void {
		console.log('渲染白色输入框，文字黑色');
	}
}

export class DarkThemeFactory extends ThemeFactory {
	createButton(): Button {
		return new DarkButton();
	}
	createInput(): Input {
		return new DarkInput();
	}
}

export class LightThemeFactory extends ThemeFactory {
	createButton(): Button {
		return new LightButton();
	}
	createInput(): Input {
		return new LightInput();
	}
}

// ==================== 请求工厂 ====================
// 抽象工厂：生成环境相关的请求对象
export abstract class RequestFactory {
	abstract createGet(url: string): GetRequest;
	abstract createPost<T = unknown>(url: string, data: T): PostRequest<T>;
}

export abstract class GetRequest {
	protected fullUrl: string;
	constructor(baseUrl: string, url: string) {
		this.fullUrl = `${baseUrl}${url}`;
	}
	abstract send(): void;
}

export abstract class PostRequest<T = unknown> {
	protected fullUrl: string;
	protected data: T;
	constructor(baseUrl: string, url: string, data: T) {
		this.fullUrl = `${baseUrl}${url}`;
		this.data = data;
	}
	abstract send(): void;
}

export class DevGetRequest extends GetRequest {
	send(): void {
		console.log(`Dev环境GET请求：${this.fullUrl}`);
	}
}

export class DevPostRequest<T = unknown> extends PostRequest<T> {
	send(): void {
		console.log(`Dev环境POST请求：${this.fullUrl}，数据：${JSON.stringify(this.data)}`);
	}
}

export class ProdGetRequest extends GetRequest {
	send(): void {
		console.log(`Prod环境GET请求：${this.fullUrl}`);
	}
}

export class ProdPostRequest<T = unknown> extends PostRequest<T> {
	send(): void {
		console.log(`Prod环境POST请求：${this.fullUrl}，数据：${JSON.stringify(this.data)}`);
	}
}

export class DevRequestFactory extends RequestFactory {
	private baseUrl = 'http://dev.api.com';
	createGet(url: string): GetRequest {
		return new DevGetRequest(this.baseUrl, url);
	}
	createPost<T = unknown>(url: string, data: T): PostRequest<T> {
		return new DevPostRequest<T>(this.baseUrl, url, data);
	}
}

export class ProdRequestFactory extends RequestFactory {
	private baseUrl = 'http://prod.api.com';
	createGet(url: string): GetRequest {
		return new ProdGetRequest(this.baseUrl, url);
	}
	createPost<T = unknown>(url: string, data: T): PostRequest<T> {
		return new ProdPostRequest<T>(this.baseUrl, url, data);
	}
}

// ==================== 演示 / 使用 ====================
export function demoTheme(): void {
	const darkTheme = new DarkThemeFactory();
	darkTheme.createButton().render();
	darkTheme.createInput().render();

	const lightTheme = new LightThemeFactory();
	lightTheme.createButton().render();
	lightTheme.createInput().render();
}

export function demoRequest(): void {
	const devFactory = new DevRequestFactory();
	devFactory.createGet('/user').send();
	devFactory.createPost('/user', { name: '张三' }).send();

	const prodFactory = new ProdRequestFactory();
	prodFactory.createGet('/health').send();
	prodFactory.createPost('/log', { event: 'start' }).send();
}

// 运行 demo（直接执行此文件时）
console.log('--- Theme Demo ---');
demoTheme();
console.log('\n--- Request Demo ---');
demoRequest();

